package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.easyViewHolder.FindDetailsEvaluationItemHolder;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.FindDetailsBean;
import com.dingwei.superiordiary.bean.MoreReplyBean;
import com.dingwei.superiordiary.mvp.presenter.MoreReplyGoodsPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/1/30.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class MoreReplyActivityGoods extends BaseActivity implements Contract.MoreReplyView {

    MoreReplyGoodsPresenter moreReplyPresenter = new MoreReplyGoodsPresenter(this);

    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.tv_user_name)
    TextView tvUserName;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.sub_layout)
    LinearLayout subLayout;
    @Bind(R.id.rl_root)
    View rlRoot;
    @Bind(R.id.ll_show_evaluation)
    LinearLayout llShowEvaluation;
    @Bind(R.id.img_give_like)
    ImageView imgGiveLike;
    @Bind(R.id.bottom_layout)
    ScrollView bottomLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.data_layout)
    LinearLayout dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    private String id;
    private String pid;

    private int page = 1;


    RecyclerArrayAdapter mAdapter = new RecyclerArrayAdapter(BaseActivity.context) {
        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return new FindDetailsEvaluationItemHolder(parent);
        }
    };

    @Override
    protected int getContentViewId() {
        return R.layout.activity_more_reply;
    }

    @Override
    protected void initData() {
        moreReplyPresenter.initData(pid, page, true, true);
    }

    @Override
    protected void initView() {

        tvTitle.setText("更多回复");
        id = getIntent().getStringExtra("id");
        pid = getIntent().getStringExtra("pid");

        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setAdapter(mAdapter);
        refreshLayout.setEnableLoadmoreWhenContentNotFull(true);

    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page+1;
                moreReplyPresenter.initData(pid, page, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                moreReplyPresenter.initData(pid, page, true, false);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.ll_show_evaluation, R.id.img_give_like,
            R.id.no_net_btn,R.id.img_show_report_dialog})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_show_evaluation:
                if (!BaseActivity.value.getIsLogin()) {
                    intent = new Intent(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                    return;
                }
                moreReplyPresenter.showEvaluationDialog(id, pid);
                break;
            case R.id.img_give_like:
                moreReplyPresenter.like(id,pid);
                break;
            case R.id.no_net_btn:
                moreReplyPresenter.initData(pid, page, true, true);
                break;
            case R.id.img_show_report_dialog:
                moreReplyPresenter.showReportDialog(this);
                break;
        }
    }

    @Override
    public void replySuccess(String content) {
        FindDetailsBean.DataBean.CommentListBean.SubBean subBean = new FindDetailsBean.DataBean.CommentListBean.SubBean();
        subBean.content = content;
        subBean.nickname = BaseActivity.value.getUserNmae();
        subBean.portrait = BaseActivity.value.getHeadImg();

        mAdapter.add(subBean);
    }

    @Override
    public void OnRefresh(boolean isRefresh, MoreReplyBean bean) {
        Glide.with(BaseActivity.context)
                .load(bean.data.portrait)
                .apply(MyApplication.headOptions)
                .into(imgHead);
        tvUserName.setText(bean.data.nickname);
        tvTime.setText(bean.data.addtime);
        tvContent.setText(bean.data.content);
        if (bean.data.is_liked==2){
            imgGiveLike.setImageResource(R.drawable.img_give_like_select);
        }else {
            imgGiveLike.setImageResource(R.drawable.img_give_like_no);
        }
        mAdapter.clear();
        mAdapter.addAll(bean.data.sub);
        refreshLayout.finishRefresh();

        showData("success");
    }

    private void showData(String state) {
        if (state.equals("success")){
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);
        }else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void OnLoadMore(boolean isRefresh, MoreReplyBean bean) {
        mAdapter.addAll(bean.data.sub);
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void Report() {
        intent = new Intent(context,ReportActivity.class);
        intent.putExtra("id",pid);
        intent.putExtra("tip_type","goods");
        startActivity(intent);
    }

    @Override
    public void likeSuccess() {
        imgGiveLike.setImageResource(R.drawable.img_give_like_select);
    }


}
